<?php
/**
 * CK 开发框架
 * User: Clake
 * Date: 15/6/11
 * Time: 23:34
 */

namespace CK\Core;
use CK\Database\DBA;
use CK\Database\Table;
use PHPUnit\Exception;

/**
 * Class Model
 * 核心模型类
 * @package CK\Core
 */
class Model extends Base{
    /**
     * @var array 初始化模型存放静态数组
     */
    protected static $_instance = [];

    /**
     * @var string 表名
     */
    protected $_table_name;

    /**
     * @var Table
     */
    protected $_table;

    private $_auto_check=false;

    private $_filters = [];

    /**
     * 初始化一个模型
     * @param $table_name string <表名称>
     *
     * @return static
     */
    public static function inst($table_name=null) {
        $class = get_called_class();
        $name = $class.($table_name?$table_name:'');
        if (!isset(self::$_instance[$name])) {
            self::$_instance[$name] = new $class($table_name);
        }
        return self::$_instance[$name];
    }

    public function __construct($table_name=null,$db='main') {
        if ($table_name !== null) {
            $this->_table_name = $table_name;
        }
        $this->_table = DBA::inst($db)->getTable($this->_table_name);
    }

    /**
     * 向表加入一条记录
     * @param array $data
     * @param bool $insert_id
     * @return bool
     */
    public function insert($data,$insert_id=true) {
        return $this->_table->insert($data,$insert_id);
    }

    /**
     * @param array $data
     * @param null $where
     *
     * @throws \Exception
     * @return bool
     */
    public function update($data,$where=null) {
        $this->_table->clear();
        if (!empty($where)) {
            return $this->_table->where($where)->update($data);
        } else {
            throw new \Exception("update must be input where condition");
        }
    }

    /**
     * 用条件删表记录
     * @param array $where
     *
     * @return bool
     */
    public function delete($where) {
        return $this->_table->clear()->where($where)->delete();
    }

    /**
     * 条件查询一条记录
     * @param $where
     *
     * @return array | bool
     */
    public function find($where) {
        return $this->_table->clear()->where($where)->execute()->get_result_one();
    }

    /**
     * 跟条件查询表记录
     * @param array $where
     *
     * @return array
     */
    public function query($fields,$where=null,$joins=null,$order=null,$number=30,$page=1,$callback=null) {
        $this->_table->clear();
        if ($fields) {
            $this->_table->select($fields);
        }

        if ($joins) {
            $this->_table->joins($joins);
        }

        if ($where) {
            $this->_table->where($where);
        }

        //总记录数
        $count = $this->_table->get_rows();
        //总分页数
        $pages = 0;

        if ($number !== 0) {
            if ($count % $number == 0) {
                $pages = intval($count / $number);
            } else {
                $pages = intval($count / $number) + 1;
            }
            $this->_table->limit($number,$page);
        }

        if ($order) {
            $this->_table->order($order[0],$order[1]);
        }

        $this->_table->execute();

        $rows = $this->_table->get_result();

        if ($callback && is_callable($callback)) {
            foreach ($rows as &$result) {
                $result = $callback($result);
            }
        }

        $list = array(
            'data'=>$rows,
            'count' => $pages,
            'length' => $count
        );
        return $list;
    }

    /**
     * 得到当前模块表名
     * @return string
     */
    public function getTableName() {
        return $this->_table_name;
    }
}